from sqlalchemy import text

from Util.DBUtil import DBUtil


class ApiExecutionLogDB:
    def __init__(self,
                 db_url="mysql+pymysql://root:d1o0N1z!rbELRf3r@rm-2vc9b10eck3rpbbv7.mysql.cn-chengdu.rds.aliyuncs.com:3306/cdp_monitor"):
        self.db_util = DBUtil(db_url)

    def insert_log(self, api_name, request_params, response_result, execution_status, error_message):
        query = text("""
        INSERT INTO cdp_monitor.api_execution_log (api_name, request_params, response_result, execution_status, error_message)
        VALUES (:api_name, :request_params, :response_result, :execution_status, :error_message)
        """)

        params = {
            'api_name': api_name,
            'request_params': request_params,
            'response_result': response_result,
            'execution_status': execution_status,
            'error_message': error_message
        }

        self.db_util.execute_update(query, params)
